package com.example.login.mvvm.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.alarm.api.AlarmApiManager
import com.example.alarm.mvvm.bean.AlarmDetailBean
import com.example.alarm.mvvm.bean.DownloadBean
import com.example.alarm.mvvm.bean.FilesAttBean
import com.gsafety.library.Base.BaseViewModel
import com.gsafety.libraryktx.bean.LoadState
import com.gsafety.libraryktx.bean.WarnData
import com.gsafety.libraryktx.expand.commonEq
import com.gsafety.libraryktx.expand.enqueue
import com.gsafety.libraryktx.net.api.request.RequestParamsManager
import kotlinx.coroutines.launch
import okhttp3.MultipartBody
import okhttp3.ResponseBody


/**
 * @author : lj
 * @date : 2020/9/23 16:53
 */
class AlarmViewModel: BaseViewModel(){
    var loadState = MutableLiveData<LoadState>()
    var loadValue = MutableLiveData<WarnData>()
    var setAttendValue = MutableLiveData<Boolean>()
    var cancelAttendValue = MutableLiveData<Boolean>()
    var addDisposalProcValue = MutableLiveData<String>()
    var currWarningDetailValue = MutableLiveData<AlarmDetailBean>()
    var uploadValue = MutableLiveData<List<FilesAttBean>>()
    var uploadState = MutableLiveData<LoadState>()
    var downloadValue = MutableLiveData<DownloadBean>()

    fun requestWarnList(size:Int, current:Int, adtevtStatus:String,key:String){
        loadState.value = LoadState.Loading()
        enqueue(AlarmApiManager.apiService.warnList(RequestParamsManager.warnList(size,current,adtevtStatus,key)),onSuccess={ result ->
            loadValue.value = result
            loadState.value = LoadState.Success()

        },onFail={ error->
            loadState.value = LoadState.Fail(error?: "")

        })
    }

    fun setAttention(pid:Int){
        enqueue(AlarmApiManager.apiService.setAttention(RequestParamsManager.setAttention(pid)),onSuccess={ result ->
            setAttendValue.value = result
        },onFail={ error->

        })
    }

    fun cancelAttention(pid:Int){
        enqueue(AlarmApiManager.apiService.cancelAttention(RequestParamsManager.setAttention(pid)),onSuccess={ result ->
            cancelAttendValue.value = result
        },onFail={ error->

        })
    }

    fun addDisposalProc(pid:Int,dispType:String,contFlag:String,dispStatus:String,dispNote:String,procUnit:String,procerInfo:String,dispRstNote:String,dispTime:String,attachments:Object){
        enqueue(AlarmApiManager.apiService.addDisposalProc(RequestParamsManager.addDisposalProc(pid,dispType,contFlag,dispStatus,dispNote,procUnit,procerInfo,dispRstNote,dispTime,attachments)),onSuccess={ result ->
            addDisposalProcValue.value = result
        },onFail={ error->

        })
    }

    fun currWarningDetail(pid:Int){
        enqueue(AlarmApiManager.apiService.currWarningDetail(RequestParamsManager.currWarningDetail(pid)),onSuccess={ result ->
            currWarningDetailValue.value = result
        },onFail={ error->

        })
    }

    fun upload(files: List<MultipartBody.Part>){
        uploadState.value = LoadState.Loading()
        enqueue(AlarmApiManager.apiService.upload(files),onSuccess={ result ->
            uploadValue.value = result
            uploadState.value = LoadState.Success()
        },onFail={ error->
            uploadState.value = LoadState.Fail(error?: "")
        })
    }

    fun download(url:String){
        commonEq(AlarmApiManager.apiService.downAttachFile(url),onSuccess={ result ->
            var downloadBean = DownloadBean()
            downloadBean.body = result
            downloadBean.url = url
            downloadValue.value = downloadBean
        },onFail={ error->

        })
    }
}